'''
Created on 2010-8-8

@author: morningbzy
'''
import copy
import random

from guanyu import GuanYu
from caocao import CaoCao
from liubei import LiuBei
from sunquan import SunQuan
from machao import MaChao
from zhangfei import ZhangFei
from zhuge import ZhuGe
from zhaoyun import ZhaoYun

class FigureList:
    GUAN_YU, LIU_BEI, SUN_QUAN, MA_CHAO, ZHANG_FEI, ZHU_GE, ZHAO_YUN,\
    CAO_CAO = range(8)
    def __init__(self):
        self.figureList = [GuanYu(), LiuBei(), SunQuan(), MaChao(), ZhangFei(),
                ZhuGe(), ZhaoYun(), CaoCao()]
        self.figureNum = len(self.figureList)
        self.figureDict = dict(zip(range(self.figureNum), self.figureList))
        self.chosenFigureDict = copy.deepcopy(self.figureDict)

    def popFigure(self):
        k = -1
        rtn = None
        while rtn is None:
            k = random.randint(0, self.figureNum - 1)
            rtn = self.chosenFigureDict.pop(k, None)
        return k, rtn
    
    def get(self, key):
        key = int(key)
        return self.figureDict.get(key)

    def getFigure(self, figId):
        return self.figureList[figId]
    
    def getCandiFiguresForZhugong(self):
        figIds = [self.CAO_CAO, self.LIU_BEI, self.SUN_QUAN]
        anotherTwo = [i for i in range(len(self.figureList)) if i not in figIds]
        random.shuffle(anotherTwo)
        figIds.extend(anotherTwo[0:2])
        rtn = []
        for i in figIds:
            rtn.append(i); rtn.append(self.figureList[i].name)
        return rtn
    
    def getCandiFiguresForOthers(self, zgFigId, nPlayers):
        figIds = [i for i in range(len(self.figureList)) if i != zgFigId]
        # make sure exactly 3*nPlayers figures are returned
        if len(self.figureList) < 3*nPlayers:
        	figIds = figIds * (3*nPlayers/len(self.figureList))
        	figIds.extend(figIds[0:3*nPlayers%len(self.figureList)])
        else:
        	figIds = figIds[0:3*nPlayers]
        random.shuffle(figIds)
        rtn = []
        for i in figIds:
            rtn.append(i); rtn.append(self.figureList[i].name)
        return rtn
		
figureList = FigureList()
